package pw.mcdarc.mc.bython.Nms;

import pw.mcdarc.mc.bython.Nms.v1_10_R1.NMS;
import pw.mcdarc.mc.bython.Bython;
import pw.mcdarc.mc.bython.Util.ReflectionUtil;

/*
This file is part of Bython.
Dont remove this header anytime.
Under the Apache2 License.
You can't sell it, repost it, share it without permission.
Date: 八月,27,2016
*/

public class getNMS {

    /**
     * 获取对应版本的NMS操作类
     * @return nms操作类实例
     */
    public static Object getNMS() {
        try {
            switch (Bython.nmsPackageName) {

                case "v1_10_R1":
                    return NMS.class.newInstance();
                case "v1_9_R2":
                    return pw.mcdarc.mc.bython.Nms.v1_9_R2.NMS.class.newInstance();
                case "v1_8_R3":
                    return pw.mcdarc.mc.bython.Nms.v1_8_R3.NMS.class.newInstance();
                default:
                    return null;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 针对无参数构造函数的获取实例方法   若构造函数有参请使用下面的方法
     * @param className 类名
     * @return 实例
     * @throws Exception
     */
    public static Object getNMSClassInstance(String className) throws Exception{
        String nmsClass = "net.minecraft.server." + Bython.nmsPackageName + "." + className;
        Class obj = Class.forName(nmsClass);
        return obj.newInstance();
    }

    /**
     * 从NMC无视版本获取类实例
     * @param className 类名
     * @param args 传参类型. 按顺序排列
     * @param initargs 传参值. 按顺序排列
     * @return 类实例
     * @throws Exception
     */
    public static Object getNMSClassInstance(String className, Class<?>[] args, Object... initargs) throws Exception {
        Class nmsClass = ReflectionUtil.getNMSClass(className);
        return ReflectionUtil.invokeConstructor(nmsClass, args, initargs);
    }

    /**
     * class数组构造方法
     * @param args 用于构造数组的Class
     * @return class数组
     */
    public static Class[] classArrayBuilder(Class<?>... args) {
        Class[] clazz = new Class[args.length];
        int i = 0;
        for (Class temp : args) {
            clazz[i] = temp;
            i ++;
        }
        return clazz;
    }

    /**
     * object数组构造方法
     * @param args 用于构造数组的Object
     * @return Object数组
     */
    public static Object[] objectArrayBuilder(Object... args) {
        Object[] objects = new Object[args.length];
        int i = 0;
        for (Object temp : args) {
            objects[i] = temp;
            i++;
        }
        return objects;
    }

    /**
     * 获取物品编辑器
     * @return 物品编辑器实例
     * @throws Exception
     */
    public static ItemEditor getItemEditor() throws Exception{
        return ItemEditor.class.newInstance();
    }
}
